/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.server;

import java.lang.String;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import uvexposureapplication.eventrouting.Consumer;
import uvexposureapplication.eventrouting.Producer;
import uvexposureapplication.eventrouting.elvin.ElvinConsumer;
import uvexposureapplication.eventrouting.elvin.ElvinProducer;
import uvexposureapplication.eventrouting.elvin.notificationlistener.ServerNotificationListener;
import uvexposureapplication.rpc.AbstractPseudoRPCServerStub;
import uvexposureapplication.rpc.servers.LocationServerPseudoRPCServerStub;
import uvexposureapplication.utils.IOStream;

/**
 *
 * @author matthieukluj
 */
public class LocationServer implements Observer {

    public Consumer consumer;
    public Producer producer;
    public Map<String, String> location;
    public ServerNotificationListener ntf;
    public AbstractPseudoRPCServerStub locationServerPseudoRPCServerStub;
    public Thread a;

    public LocationServer(String confFile, String server) {
        this.ntf = new ServerNotificationListener();
        this.ntf.addObserver(this);

        this.location = IOStream.readLocationsData(confFile);
        this.consumer = new ElvinConsumer(server, this.ntf);
        this.producer = new ElvinProducer(server);

        this.consumer.subscribe("Group == 'Sensor' && Type == 'Location'");

        this.locationServerPseudoRPCServerStub = new LocationServerPseudoRPCServerStub(this, "fetchIndoorLocation", server);
        a = new Thread(this.locationServerPseudoRPCServerStub);
        a.start();
    }

    public String fetchIndoorLocation() {
        String indoorLocation = "";
        for (Map.Entry<String, String> locationEntry : this.location.entrySet()) {
            if (locationEntry.getValue().equals("Indoor")) {
                indoorLocation += locationEntry.getKey() + "-";
            }
        }
        indoorLocation = indoorLocation.substring(0, indoorLocation.length() - 1);
        System.out.println("[SENDING] Indoor Location TO ContextManager");
        return indoorLocation;
    }

    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("java LocationServer [configuration-file][ElvinURL]");
            System.exit(0);
        }

        LocationServer ls = new LocationServer(args[0], args[1]);
    }

    @Override
    public void update(Observable o, Object arg) {
        if (arg instanceof Map) {
            Map<String, String> argMap = (Map<String, String>) arg;

            Map<String, String> notification = new HashMap<String, String>();
            notification.put("Group", "LocationServer");
            notification.put("UserName", (String) argMap.get("UserName"));
            notification.put("Coordinate", (String) argMap.get("Value"));
            notification.put("Value", this.location.get((String) argMap.get("Value")));
            //System.out.println(argMap.get("Group") + " - " + (String) argMap.get("Value"));
            System.out.println("[SENDING] Current location TO ContextManager");
            this.producer.produceFromCollection(notification);
        }
    }

    public void shutDown() {
        this.consumer.closeConnection();
        this.producer.closeConnection();
        a.interrupt();
        System.exit(0);
    }
}
